#include "StdAfx.h"
#include "SMS_Business.h"
#include "DBFactory/DataSetSmsAlarm.h"


CSMS_Business::CSMS_Business(void)
:db(CDbFactory::CreateDb())
{

}

CSMS_Business::~CSMS_Business(void)
{
	CDbFactory::ReleaseDb(db);
}

CString CSMS_Business::GetUserIDByPhoneNumber( CString& var_number )
{
	std::string str_number=CStringToStdString(var_number);
	std::string str_sql="select userid from user where telephone like '";
	str_sql.append(str_number);
	str_sql.append("'");

	otl_stream s(1,str_sql.c_str(),*db);

	while(!s.eof())
	{
		char userid[32];
		s>>userid;
		CString str_userid(userid);
		return str_userid;
	}

	return _T("");
}

CString CSMS_Business::GetProtorolByUserid( CString& var_userid )
{
	
	std::string str_userid=CStringToStdString(var_userid.Trim());
	std::string str_sql="select protocol from user where userid='";
	str_sql.append(str_userid);
	str_sql.append("'");

	otl_stream s(1,str_sql.c_str(),*db);

	while(!s.eof())
	{
		char protocol[32];
		s>>protocol;
		CString str_protocol(protocol);
		return str_protocol;
	}

	return _T("");
}

std::string  CSMS_Business::GetPhoneByUserid( CString& var_userid )
{
	std::string str_userid=CStringToStdString(var_userid);
	std::string str_sql="select telephone from user where userid='";
	str_sql.append(str_userid);
	str_sql.append("'");

	otl_stream s(1,str_sql.c_str(),*db);

	while(!s.eof())
	{
		char phonenumber[32];
		s>>phonenumber;
		std::string  str_phonenumber(phonenumber);
		return str_phonenumber;
	}

	return "";
}

void CSMS_Business::DeleteBySmsAlarmid( CString& var_id )
{
	std::string str_sql="delete from SmsAlarm where id=";
	str_sql.append(CStringToStdString(var_id));
	otl_cursor::direct_exec (*db,str_sql.c_str(),	otl_exception::disabled); 
}

void CSMS_Business::AdditionSmsAlarm( CString& var_phonenumber,CString& var_type,CString& var_content,CString& var_datetime )
{
    CString str_userid=GetUserIDByPhoneNumber(var_phonenumber);

	CDataSet_SmsAlarm SmsAlarmSet;
	SmsAlarmSet.Open();
	SmsAlarmSet.AddNew();
	SmsAlarmSet.m_userid=str_userid;
	SmsAlarmSet.m_phonenumber=var_phonenumber;
	SmsAlarmSet.m_type=var_type;
	SmsAlarmSet.m_content=var_content;
	SmsAlarmSet.m_time=var_datetime;
	SmsAlarmSet.Update();
	SmsAlarmSet.Close();	
}

otl_stream* CSMS_Business::GetSmsAlarmStream( void )
{
	otl_stream *ps=new otl_stream(50,"select * from SmsAlarm",*db);
	return ps;
}

void CSMS_Business::ChangSmsToTcp( CString& var_userid )
{
	std::string str_sql="update user set protocol='TCP' where userid='";
	str_sql.append(CStringToStdString(var_userid));
	str_sql.append("'");
	int i=otl_cursor::direct_exec (*db,str_sql.c_str(),	otl_exception::disabled); 
}